package org.study.javafx.study01.notebook.xml.controller;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.resource.ClassPathResource;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.TextArea;
import lombok.extern.slf4j.Slf4j;

import javax.swing.filechooser.FileSystemView;
import java.io.File;
import java.net.URL;
import java.util.ResourceBundle;

/**
 * @author Administrator
 */
@Slf4j
public class NotebookController implements Initializable {
    final String DESKTOP = FileSystemView.getFileSystemView().getHomeDirectory().getPath();
    @FXML
    private TextArea txtContent;
    @FXML
    private Button btnSave;

    /**
     * 程序初次加载时会执行的方法
     */
    @Override
    public void initialize(URL location, ResourceBundle resources) {
        // 将文件中的内容读取展示到文本控件
        txtContent.setText(FileUtil.readUtf8String(new ClassPathResource("data.txt").getFile()));
    }

    @FXML
    public void onSaveClicked(ActionEvent event) {
        if ("保 存".equals(btnSave.getText())) {
            FileUtil.writeUtf8String(txtContent.getText(), DESKTOP + File.separator + System.currentTimeMillis() + ".txt");
            btnSave.setText("编 辑");
            txtContent.setEditable(false);
        } else {
            btnSave.setText("保 存");
            txtContent.setEditable(true);
        }
    }
}
